package ssm.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;

import com.alibaba.fastjson.JSON;
import ssm.entity.Manager;
import ssm.service.ManagerService;

@Controller
public class ManagerControl {
	
	@Autowired
	private ManagerService managerService;
	
	
	@RequestMapping("/adminlogin")
	public String adminLogin() {
		return "login/adminlogin";
	}
	
	
	@RequestMapping("/adminout")
	@ResponseBody
	public String userLogin(SessionStatus sessionStatus) {
		sessionStatus.setComplete();
		return "1";
	}
	
	@RequestMapping("/checkusername")
	@ResponseBody
	public String checkusername(HttpServletRequest request) {
		Manager manager = new Manager();
		manager.setmAccount(request.getParameter("username"));
		if(managerService.checkUsername(manager)!=null) {
			manager = managerService.getManager(manager);
			return "1";
		}else {
			return "0";
		}
	}
	
	@RequestMapping("/adminloginsubmit")
	@ResponseBody
	public String adminLoginSubmit(HttpServletRequest request,HttpSession session,Model model) {
		Manager manager = new Manager();
		manager.setmAccount(request.getParameter("username"));
		manager.setmPass(request.getParameter("password"));
		if(managerService.isManager(manager)) {
			manager = managerService.getManager(manager);
			System.out.println(manager);
			session.setAttribute("curManager", manager);
			return "1";
		}else {
			return "0";
		}
	}
	
	@RequestMapping(value="/getmanager",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String getManager(HttpServletRequest request,HttpSession session) {
		HttpSession request2 = request.getSession();
		Manager curManager = (Manager) request2.getAttribute("curManager");
		/*System.out.println(curManager.getmAccount());
		System.out.println(JSON.toJSONString(curManager));*/
		return JSON.toJSONString(curManager);
	}
	
	
}
